package com.eastdigit.bpm.controller;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eastdigit.bpm.service.ProcessService;
import com.eastdigit.servlet.FieldAlias;
import com.eastdigit.servlet.ReqBean;
import com.eastdigit.servlet.RequestAttribute;
import com.eastdigit.servlet.Response;

/**
 * Created by Administrator on 2015/9/21.
 */
@Controller
@RequestMapping(value = "/bpm")
public class ProcessController {

	@Autowired
	private ProcessService processService;

	/**
	 * 获取流程列表
	 * 
	 * @param reqBean
	 * @return
	 */
	@RequestMapping(value = "/process", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject list(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
		return Response.response(processService.getProcessDefinitions(reqBean));
	}
	

	/**
	 * 根据流程部署ID删除流程
	 * 
	 * @param deploymentId
	 * @return
	 */
	@RequestMapping(value = "/process/{deploymentId}/deleteDeployment")
	@ResponseBody
	public JSONObject deleteDeployment(@PathVariable String deploymentId) {
		processService.deleteDeployment(deploymentId);
		return Response.responseSuccess();
	}
	
	/**
     * 根据流程定义key删除流程
     * 
     * @param processDefinitionKey
     * @return
     */
    @RequestMapping(value = "/process/{processDefinitionKey}/deleteProcessDefinitionByKey")
    @ResponseBody
    public JSONObject deleteProcessDefinitionByKey(
            @PathVariable String processDefinitionKey) {
        processService.deleteProcessDefinitionByKey(processDefinitionKey);
        return Response.responseSuccess();
    }
	
	/**
	 * 流程实例列表
	 * 
	 * @param reqBean
	 * @return
	 */
	@RequestMapping(value = "/processInstance/list")
	@ResponseBody
	public JSONObject getProcessInstancees(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
		return Response.response(processService.getProcessInstances(reqBean));
	}
	
	/**
	 * 我的请求流程
	 * 
	 * @param reqBean
	 * @return
	 */
	@RequestMapping(value = "/processInstance/list/my")
	@ResponseBody
	public JSONObject getMyStartProcessInstancees(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
		reqBean.addInfo("starter", reqBean.getUserId());
		return Response.response(processService.getProcessInstances(reqBean));
	}
	
	/**
	 * 我的已经办结流程
	 * 
	 * @param reqBean
	 * @return
	 */
	@RequestMapping(value = "/processInstance/list/mydone")
	@ResponseBody
	public JSONObject getMyDoneProcessInstancees(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
		reqBean.addInfo("assignee", reqBean.getUserId());
		reqBean.addInfo("finished", "true");
		return Response.response(processService.getProcessInstances(reqBean));
	}
	
	
	
	/**
     * 初始启动表单
     * 
     * @param processDefinitionKey
     * @return
     */
    @RequestMapping(value = "/process/{processDefinitionKey}/start-form")
    @ResponseBody
    public JSONObject getStartForm(@PathVariable String processDefinitionKey, @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(processService
                .getStartForm(processDefinitionKey, reqBean));
    }
    
    /**
     * 启动流程
     * @param processDefinitionKey
     * @param properties
     * @return
     */
    @RequestMapping(value = "/process/{processDefinitionId}/start")
    @ResponseBody
    public JSONObject startProcessById(
            @PathVariable String processDefinitionId,
            @RequestParam String properties, @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        processService.submitAndStartProcessById(processDefinitionId, null, properties, reqBean.getUserId());
        return Response.responseSuccess();
    }

	/**
	 * 启动流程
	 * @param processDefinitionKey
	 * @param properties
	 * @return
	 */
	@RequestMapping(value = "/process/{processDefinitionKey}/startByKey")
	@ResponseBody
	public JSONObject startProcessByKey(
			@PathVariable String processDefinitionKey,
			@RequestParam String properties, @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
		processService.submitAndStartProcessByKey(processDefinitionKey, null, properties, reqBean.getUserId());
		return Response.responseSuccess();
	}

	/**
	 * 流程实例浏览
	 * @param processDefinitionId
	 * @param properties
	 * @return
	 */
	@RequestMapping(value = "/process/{processInstanceId}/view")
	@ResponseBody
	public JSONObject viewProcess(@PathVariable String processInstanceId) {
		return Response.response(processService
				.getProcessInfo(processInstanceId));
	}

	/**
	 * 流程轨迹跟踪
	 * @param processDefinitionId
	 * @param properties
	 * @return
	 */
	@RequestMapping(value = "/process/{processInstanceId}/track")
	@ResponseBody
	@FieldAlias(service = "userService", field = "assignee", alias = "assigneeName")
	public JSONObject trackProcess(@PathVariable String processInstanceId) {
		return Response.response(processService
				.getProcessTracks(processInstanceId));
	}

	/**
	 * 删除流程实例
	 * @param processInstanceId
	 * @return
	 */
	@RequestMapping(value = "/process/{processInstanceId}/delete")
	@ResponseBody
	public JSONObject deleteProcess(@PathVariable String processInstanceId,@RequestAttribute(ReqBean.NAME) ReqBean reqBean) throws Exception{
		processService.deleteProcess(processInstanceId,Boolean.valueOf(reqBean.getInfo("deleteBusiness")));
		return Response.responseSuccess();
	}
	
	/**
     * 获取流程审批意见
     * 
     * @param taskId
     * @return
     */
    @RequestMapping(value = "/process/comments/{processInstanceId}")
    @ResponseBody
    @FieldAlias(service = "userService", field = "userId", alias = "userName")
    public JSONObject processComments(@PathVariable String processInstanceId, @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(processService.getProcessComments(processInstanceId));
    }
    
    /**
     * 获取流程打印数据
     * @param processInstanceId
     * @return
     */
    @RequestMapping(value = "/process/{processInstanceId}/print")
	@ResponseBody
	public JSONObject printProcess(@PathVariable String processInstanceId) {
    	JSONObject processInfo = processService.getProcessInfo(processInstanceId);
    	JSONObject data = processInfo.containsKey("form") ? processInfo.getJSONObject("form") : new JSONObject();
    	JSONArray comments = processInfo.containsKey("comments") ? processInfo.getJSONArray("comments") : null;
    	if(comments != null && !comments.isEmpty()){
    		for(Object comment : comments){
    			JSONObject comm = (JSONObject)comment;
    			String type = comm.containsKey("type") ? comm.getString("type") : "";
    			String commentTime = comm.containsKey("commentTime") ? comm.getString("commentTime") : "";
    			String message = comm.containsKey("message") ? comm.getString("message") : "";
    			String userId = comm.containsKey("userId") ? comm.getString("userId") : "";
    			JSONArray _commentsOfKey  =  data.containsKey(type) ? data.getJSONArray(type) : new JSONArray();
    			_commentsOfKey.add(userId + "[" + commentTime + "]:" + message);
    			data.put(type, _commentsOfKey);
    		}
    	}
    	JSONObject result = new JSONObject();
    	result.put("fileName", processInfo.getString("processDefinitionKey"));
    	result.put("data", data);
		return Response.response(result);
	}
}
